// Blob Buffer
// (c) jimon game studio

#ifndef JEH_CBLOBBUFFER
#define JEH_CBLOBBUFFER

#include "IBlobBuffer.h"
#include "sqlite3.h"

namespace je
{
	namespace core
	{
		//! Blob Buffer
		class CBlobBuffer:public je::core::IBlobBuffer
		{
		private:
			//! Blob
			sqlite3_blob * Blob;

			//! Size
			s32 Size;

			//! Lock Mode
			BufferLockType LockMode;

			//! True If Locked
			u1 Locked;

			//! Position
			u32 Position;
		public:
			//! Constructor
			CBlobBuffer(sqlite3_blob * OpenBlob);

			//! Destructor
			~CBlobBuffer();

			//! Lock Buffer
			void Lock(BufferLockType LockType)
			{
				JEASSERT(!Locked)

				Locked = true;
				LockMode = LockType;
			}

			//! UnLock Buffer
			void UnLock()
			{
				Locked = false;
			}

			//! Return True if locked
			u1 IsLocked()
			{
				return Locked;
			}

			//! Read Data
			void Read(void * Pointer,u32 Bytes);

			//! Write Data
			void Write(const void * Pointer,u32 Bytes);

			//! Allocate Buffer for data,size is count of objects
			void Allocate(u32 NewSize);

			//! DeAllocate Buffer
			void DeAllocate();

			//! Set Position
			void SetPosition(u32 NewPosition,u1 Relative = false)
			{
				if(Relative)
					Position += NewPosition;
				else
					Position = NewPosition;
			}

			//! Get Position
			u32 GetPosition()
			{
				return Position;
			}

			//! Get Size in bytes
			u32 GetSize()
			{
				return Size;
			}

			//! Get Size of one object
			u32 GetObjectSize()
			{
				return sizeof(c8);
			}
		};
	}
}

#endif
